var app = getApp();
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["所有", "未使用", "已使用"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    list:[],
    list1:[],
    list2:[],
    id:0,
    page:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });

    var id = options.id //今日订单 1今日成交 2昨日成交 3等待核销 4

    if(id == 4){
      
      that.setData({
        id: id,
        activeIndex:1,
        sliderOffset:125
      })
      Getglist1(that, 1, app.globalData.userid, id)
    }else{

      that.setData({
        id: id,
      })
      Getglist(that, 1, app.globalData.userid, id)
    }
    
   

  },
  tabClick: function (e) {
    var that = this
    that.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    console.log(e.currentTarget.offsetLeft)
    if (e.currentTarget.id ==1){
      //未使用，等待核销
      that.setData({
        page: 1
      })
      Getglist1(that,1,app.globalData.userid, that.data.id)
    }
    if (e.currentTarget.id == 2){
      //已使用
      that.setData({
        page: 1
      })
      Getglist2(that, 1, app.globalData.userid, that.data.id)
    }

    if (e.currentTarget.id == 0){
      that.setData({
        id: 0,
        page:1
      })
      //所有
      Getglist(that,1,app.globalData.userid,0)
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this 
    if(that.data.activeIndex == 0){
      Getglist(that, that.data.page, app.globalData.userid, that.data.id)
    }
    if (that.data.activeIndex == 1) {
      Getglist1(that, that.data.page, app.globalData.userid, that.data.id)
    }
    if (that.data.activeIndex == 2) {
      Getglist2(that, that.data.page, app.globalData.userid, that.data.id)
    }
   
  },

})


var url = "https://17fx.com/weapp/Adminapi/gettodayorder.html";
var page_size = 20
//今日订单 1今日成交 2昨日成交 3
var Getglist = function (that,page,user_id,id) {
  wx.request({
    url: url,
    data: {
      user_id: app.globalData.userid,
      id:id,
      page:page,
      page_size: page_size
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      console.log(res.data);
      if (page == 1) {
        that.setData({
          list: []
        })
      }
      if (res.data != null) {
        var list = that.data.list;
        for (var i = 0; i < res.data.length; i++) {
          list.push(res.data[i]);
        }
        that.setData({
          list: list,
          page: that.data.page + 1
        })
      }
      
    }
  })
}

var url1 = "https://17fx.com/weapp/Adminapi/gettodayorder1.html";
//未使用 等待核销 4
var Getglist1 = function (that, page, user_id, id) {
  wx.request({
    url: url1,
    data: {
      user_id: app.globalData.userid,
      id: id,
      page: page,
      page_size: page_size
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      console.log(res.data);
      if (page == 1) {
        that.setData({
          list1: []
        })
      }
      if (res.data != null) {
        var list1 = that.data.list1;
        for (var i = 0; i < res.data.length; i++) {
          list1.push(res.data[i]);
        }
        that.setData({
          list1: list1,
          page: that.data.page + 1
        })
      }

    }
  })
}

var url2 = "https://17fx.com/weapp/Adminapi/gettodayorder2.html";
//未使用 等待核销 4
var Getglist2 = function (that, page, user_id, id) {
  wx.request({
    url: url2,
    data: {
      user_id: app.globalData.userid,
      id: id,
      page: page,
      page_size: page_size
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      console.log(res.data);
      if (page == 1) {
        that.setData({
          list2: []
        })
      }
      if (res.data != null) {
        var list2 = that.data.list2;
        for (var i = 0; i < res.data.length; i++) {
          list2.push(res.data[i]);
        }
        that.setData({
          list2: list2,
          page: that.data.page + 1
        })
      }

    }
  })
}